package sqlserver;

import java.io.File;
import java.sql.Connection;

import org.firefly.db.util.DBInfo;
import org.firefly.db.util.DBName;
import org.firefly.db.util.DBUtils;


public class Eadpv5 {

	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		Class.forName("oracle.jdbc.driver.OracleDriver");
		Class.forName("net.sourceforge.jtds.jdbc.Driver");
//		Class.forName("com.mysql.jdbc.Driver");
		Connection srcConn = java.sql.DriverManager.getConnection("jdbc:jtds:sqlserver://172.16.1.7:1433/eadp5_4", "sa", "eplugger");
//		Connection tarConn = java.sql.DriverManager.getConnection("jdbc:mysql://172.16.1.104:3306/eadp5_4?useUnicode=true&characterEncoding=utf-8&autoReconnect=true", "eadp5_4", "123456");
		Connection tarConn = java.sql.DriverManager.getConnection("jdbc:oracle:thin:@172.16.1.3:1521:orcl", "eadp5_4", "eplugger");
//		Connection tarConn = java.sql.DriverManager.getConnection("jdbc:mysql://127.0.0.1:8889/epSecurity?useUnicode=true&characterEncoding=utf-8&autoReconnect=true", "eplugger", "123456");
		try {
			DBInfo srcInfo = new DBInfo(srcConn, null, DBName.SqlServer2005);
//			DBInfo tarInfo = new DBInfo(tarConn, null, DBName.Mysql);
			DBInfo tarInfo = new DBInfo(tarConn, "EADP5_4", DBName.Oracle);
//			DBUtils.dbConvertA2B(srcInfo, tarInfo, true);
			File sqlFile = new File("/Users/firefly/Desktop/eadp5_4_oracle_all.sql");
			DBUtils.dbConvertA2SqlFile(tarInfo, sqlFile);
//			DBUtils.dbConvertA2B(srcInfo, tarInfo, true);
//			Connection con  = tarInfo.getConn();
//			try {
//				Statement s = con.createStatement();
//				PreparedStatement ps = con.prepareStatement("update BIZ_PROJECT set serverIds=? where custom_id=?");
//				ResultSet rs = s.executeQuery("select id,customid from BIZ_CUSTOM_SERVER order by customid");
//				List<String> ls = new ArrayList<String>();
//				String ci = null;
//				int i = 0;
//				while(rs.next()){
//					i ++; 
//					System.out.println("处理了" + i + "行");
//					String id = rs.getString("id");
//					String customid = rs.getString("customid");
//					if(ci == null){
//						ci = customid;
//						ls.add(id);
//					}else if(ci.equals(customid)){
//						ls.add(id);
//					}else if(!ci.equals(customid)){
//						ps.setString(1, StringUtils.join(ls,","));
//						ps.setString(2, ci);
//						ps.executeUpdate();
//						ls.clear();
//						ci = customid;
//						ls.add(id);
//					}
//				}
//			} catch (Exception e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			} finally{
//				con.close();
//			}
			
			/*转移表*/
//			DBUtils.tableConvertA2B(srcInfo, tarInfo, new String[]{"CFG_CATEGORY","CFG_CATEGORY_ENTRY","CFG_CATEGORY_MAPPING","DM_AREA","DM_CHECK","DM_QUICK_SEARCH","SYS_AUTHORITY","SYS_BUSINESS_FILTER","SYS_BUSINESS_FILTER_RESULT","SYS_CFG_EDIT_TABLE","SYS_CFG_EXPORT","SYS_CFG_FORM","SYS_CFG_SCENE","SYS_CFG_SQL_TABLE","SYS_CFG_STAT","SYS_CFG_TABLE","SYS_CONCURRENT","SYS_ENTITY_META","SYS_GROUP","SYS_MENU","SYS_MODULE","SYS_PARAM"});
//			DBUtils.tableConvertA2B(srcInfo, tarInfo, new String[]{"SYS_MENU"});
//			DBUtils.tableConvertA2B(srcInfo, tarInfo, new String[]{"SYS_GROUP"});
			
			/*比较
			FileOutputStream fos = new FileOutputStream("/Users/firefly/Desktop/a.sql");
			OutputStreamWriter osw = new OutputStreamWriter(fos);
			DBUtils.compareDB(srcInfo, tarInfo, osw, true);
			*/
			/*导出insert sql */
//			FileOutputStream fos = new FileOutputStream("/Users/firefly/Desktop/EADPV5.2.sql");
//			OutputStreamWriter osw = new OutputStreamWriter(fos);
////			DBUtils.expertInsertSQL(tarInfo, tarInfo.getDialect(), osw, new String[]{"SYS_CFG_EDIT_TABLE","SYS_CFG_EXPORT","SYS_CFG_FORM","SYS_CFG_SCENE","SYS_CFG_SQL_TABLE","SYS_CFG_STAT","SYS_CFG_TABLE","SYS_ENTITY_META"}, null, true);
//			DBUtils.expertInsertSQL(tarInfo, tarInfo.getDialect(), osw, null, null, true);
			
//			DBUtils.compareDB(srcInfo, tarInfo, osw, true);
//			File sqlFile = new File("/Users/firefly/Desktop/eadp5_1.sql");
//			DBUtils.dbConvertA2SqlFile(srcInfo, sqlFile);
		} finally{
			srcConn.close();
			tarConn.close();
		}
	}

}
